package com.chzc.sys.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.chzc.po.Menu;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>
 * 菜单信息表 Mapper 接口
 * </p>
 *
 * @author CV大魔王
 * @since 2021-05-24
 */
@Mapper
public interface MenuMapper extends BaseMapper<Menu> {


    /**
     * 根据角色id查询权限码
     * @param roleId 角色id
     * @return 权限码
     */
    @Select("SELECT DISTINCT sys_menu.code from sys_menu , sys_role_menu " +
            " WHERE sys_menu.id = sys_role_menu.menu_id " +
            " AND sys_role_menu.role_id = #{roleId} ")
    List<String> findMenuCodeByRoleId(String roleId);


    /**
     * 查询指定用户ID的所拥有的权限（目录、菜单、按钮）
     * @param userId 用户id
     * @return 用户的菜单列表
     */
    @Select("SELECT DISTINCT m.id,m.parent_id,m.NAME,m.CODE,m.url,m.type,m.icon,m.remark,m.sort,m.create_date,m.update_date " +
            " FROM sys_user AS u JOIN sys_user_role AS ur " +
            " ON u.id = ur.user_id JOIN sys_role AS r " +
            " ON ur.role_id = r.id JOIN sys_role_menu AS rm " +
            " ON rm.role_id = r.id JOIN sys_menu AS m " +
            " ON rm.menu_id = m.id WHERE u.id = #{userId} ORDER BY m.sort")
    List<Menu> findByUserId(@Param("userId") String userId);
}
